package com.youfan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.youfan.entity.Brand;
import com.youfan.mapper.BrandMapper;
import com.youfan.page.CommonPage;
import com.youfan.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public int insertOne(Brand youfanBrand) {
        youfanBrand.setCreateTime(new Date());

        return brandMapper.insert(youfanBrand);
    }

    @Override
    public CommonPage<Brand> queryByCondtion(Brand youfanBrand) {
        PageHelper.startPage(youfanBrand.getPageNum(),youfanBrand.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(youfanBrand.getBrandname())){
            queryWrapper.like("brandname",youfanBrand.getBrandname());
        }
        if(StringUtils.isNotBlank(youfanBrand.getFirstLetter())){
            queryWrapper.like("first_letter", youfanBrand.getFirstLetter());
        }
        List<Brand> list = brandMapper.selectList(queryWrapper);
        return CommonPage.restPage(list);
    }

    @Override
    public Brand findByid(Brand youfanBrand) {
        return  brandMapper.selectById(youfanBrand.getId());
    }

    @Override
    public int updateOne(Brand youfanBrand) {
        youfanBrand.setUpdateTime(new Date());
        return brandMapper.updateById(youfanBrand);
    }
}
